package com.um.jdy.platform.station.manager;

import com.um.jdy.common.device.entity.po.Fee;
import com.um.jdy.common.device.entity.vo.FeeVO;
import com.um.jdy.common.device.service.*;
import com.um.jdy.platform.station.entity.vo.FeeListVO;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class FeeManager extends BaseManager<FeeService> {

    @Autowired
    FeeEbPriceService feeEbPriceService;

    @Autowired
    private FeeEbPowerService feeEbPowerService;

    @Autowired
    private FeeEBoxPriceService feeEBoxPriceService;

    @Autowired
    private FeeEBoxPowerService feeEBoxPowerService;

    @Autowired
    private FeePlaceTimeService feePlaceTimeService;

    @Autowired
    private FeePlaceService feePlaceService;

    @Autowired
    private FeeEvPriceService feeEvPriceService;

    @Autowired
    private FeeEvTimeService feeEvTimeService;

    @Autowired
    private FeePbPriceService feePbPriceService;

    @Autowired
    private FeeEbTimeService feeEbTimeService;

    @Autowired
    private FeeBsbRentService feeBsbRentService;

    @Autowired
    private FeeBsbSwapService feeBsbSwapService;


    public Response<Page<FeeListVO>> getFeePage(PageParams params) {
        Page page = new Page();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        params.getCondition().put("s.com_id", getComId());
        params.getCondition().put("orderby", "create_time DESC");
        page = service.getCMSViewModelPageBySqlName(page, "get_fee_page", "get_fee_count", FeeListVO.class, params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());

        return Response.success(page);
    }

    @Transactional
    public Response delete(String id) {
        Fee fee = service.getEntity(id);
        if(fee.getDeviceType().equals(DeviceTypeEnum.EB.getKey())){

            feeEbPriceService.delete("device_fee_id",id);
            feeEbPowerService.delete("device_fee_id",id);

        }else if(fee.getDeviceType().equals(DeviceTypeEnum.EBox.getKey())){

            feeEBoxPriceService.delete("device_fee_id",id);
            feeEBoxPowerService.delete("device_fee_id",id);
            feePlaceService.delete("device_fee_id",id);
            feePlaceTimeService.delete("device_fee_id",id);

        }else if(fee.getDeviceType().equals(DeviceTypeEnum.EV.getKey())){

            feeEvPriceService.delete("device_fee_id",id);
            feeEvTimeService.delete("device_fee_id",id);
            feePlaceService.delete("device_fee_id",id);
            feePlaceTimeService.delete("device_fee_id",id);


        }else if(fee.getDeviceType().equals(DeviceTypeEnum.PB.getKey())){

            feePbPriceService.delete("device_fee_id",id);

        }
        boolean result = this.service.delete(id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        for(String id : ids){
            Fee fee = service.getEntity(id);
            if(fee.getDeviceType().equals(DeviceTypeEnum.EB.getKey())){

                feeEbPriceService.delete("device_fee_id",id);
                feeEbPowerService.delete("device_fee_id",id);
                feeEbTimeService.delete("device_fee_id",id);

            }else if(fee.getDeviceType().equals(DeviceTypeEnum.EBox.getKey())){

                feeEBoxPriceService.delete("device_fee_id",id);
                feeEBoxPowerService.delete("device_fee_id",id);
                feePlaceService.delete("device_fee_id",id);
                feePlaceTimeService.delete("device_fee_id",id);


            }else if(fee.getDeviceType().equals(DeviceTypeEnum.EV.getKey())){

                feeEvPriceService.delete("device_fee_id",id);
                feeEvTimeService.delete("device_fee_id",id);
                feePlaceService.delete("device_fee_id",id);
                feePlaceTimeService.delete("device_fee_id",id);

            }else if(fee.getDeviceType().equals(DeviceTypeEnum.PB.getKey())){

                feePbPriceService.delete("device_fee_id",id);

            }else if(fee.getDeviceType().equals(DeviceTypeEnum.BSB.getKey())){

                feeBsbRentService.delete("device_fee_id",id);
                feeBsbSwapService.delete("device_fee_id",id);

            }
        }
        boolean result = service.deleteIds(ids);
        return result ? Response.success() : Response.failed();
    }

    public Response<List<FeeVO>> getFeeList(String merchantId,String deviceType){
        List<Fee> list = service.getList(new String[]{"platform_merchant_id","device_type"},new Object[]{merchantId,deviceType},new String[]{"create_time"},new String[]{"desc"});
        return Response.success(UMBeanUtils.copyBeanList(list,FeeVO.class));
    }
}
